<?php

namespace App\Console\Commands;

use App\Models\Constant\OrderConstant;
use App\Models\Order\Order;
use App\Models\User\LiPei;
use App\Models\User\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class AutoCancel extends Command
{

    protected $signature = 'check:cancel';

    public function handle() {
        //Log::info('check:cancel');
        $orders = Order::where(['status'=>OrderConstant::STATUS_WAIT_RECEIVING])->get();
        $currTime = date('Y-m-d H:i:s');
        foreach ($orders as $order) {
            $afterTime = date('Y-m-d H:i:s',strtotime($order->updated_at." +6 hours"));
            if($currTime>$afterTime){
                Log::info('check:cancel==>id'.$order->id);
                $order->status = OrderConstant::STATUS_CANCEL;
                $order->save();

                $user = User::where('id',$order->user_id)->first();
                if(!empty($user)){
                    $phone = $user->phone;
                    $template= 'SMS_251695573';
                    $verifyCode =0;
                    sendSms($phone,$template,$verifyCode);
                }else{
                    Log::info('check:cancel==>id'.$order->id.'未找到有效用户');
                }
            }
        }

        return 0;
    }
}
